//
//  CHReviewedController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/5.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHReviewedController.h"
#import "CHAuditContentController.h"
#import "CHAuditDetailController.h"
#import "CHMenuView.h"
#import "CHCheckCell.h"
#import "CHNoCheckCell.h"
#import "CHNoCheckModel.h"
#import "CHCheckModel.h"
@interface CHReviewedController ()<CHMenuViewDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) CHMenuView *menuView1;
@property (nonatomic ,strong) CHMenuView *menuView2;
@property (nonatomic ,strong) UITableView *dsh_tableView;
@property (nonatomic ,strong) UITableView *ysh_tableView;
@property (nonatomic ,strong) UIScrollView *mainScrollView;
@property (nonatomic ,strong) NSMutableArray *nocheckDatas;
@property (nonatomic ,strong) NSMutableArray *checkDatas;
@property (nonatomic ,assign) int index;
@property (nonatomic ,assign) int page;    // 页码
@property (nonatomic ,assign) NSInteger type;   // 审核状态

@end

@implementation CHReviewedController
-(NSMutableArray *)nocheckDatas{
    
    if (!_nocheckDatas) {
        _nocheckDatas = [NSMutableArray array];
    }
    
    return _nocheckDatas;
}

-(NSMutableArray *)checkDatas{
    if (!_checkDatas) {
        _checkDatas = [NSMutableArray array];
    }
    return _checkDatas;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"审核";
    self.view.backgroundColor = KRGB(239, 239, 249);
    _type = 0;
    
    [self setupTitleMenu];
    
    [self setupMainScrollView];
    
    [self getNoCheckData];
   
    [self getCheckData];
    
}


-(void)getCheckData{
    _page = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"company"] = @"";
    params[@"user"] = [Globle getInstance].user;
    if (_type == 0) {
        params[@"type"] = @"";
    }else{
        params[@"type"] = [NSString stringWithFormat:@"%zd",_type];
    }
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappcheckedcompanylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.checkDatas removeAllObjects];
     
        
        [self.ysh_tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHCheckModel *model = [CHCheckModel objectWithKeyValues:dict];
                [self.checkDatas addObject:model];
            }
            self.ysh_tableView.placeholderImage = @"data_null";
            self.ysh_tableView.placeholder = @"暂时没有数据哦~";
            [self.ysh_tableView reloadData];
            
        }
    }];
    
    
}

-(void)getNoCheckData{
    _index = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"company"] = @"";
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_index];
    [[Globle getInstance].service requestWithServiceName:@"lcipappcheckcompanylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.nocheckDatas removeAllObjects];

        [self.dsh_tableView.mj_header endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHNoCheckModel *model = [CHNoCheckModel objectWithKeyValues:dict];
                [self.nocheckDatas addObject:model];
            }
            self.dsh_tableView.placeholderImage = @"data_null";
            self.dsh_tableView.placeholder = @"暂时没有数据哦~";
            [self.dsh_tableView reloadData];
            
        }
    }];
}


//  加载更多待审核数据
-(void)loadMoreNoCheckData{
    _index++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"company"] = @"";
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_index];
    [[Globle getInstance].service requestWithServiceName:@"lcipappcheckcompanylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        

        [self.dsh_tableView.mj_footer endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHNoCheckModel *model = [CHNoCheckModel objectWithKeyValues:dict];
                [self.nocheckDatas addObject:model];
            }
            [self.dsh_tableView reloadData];
            
        }
    }];
    
}



-(void)loadMoreCheckData{
    _page++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"company"] = @"";
    params[@"user"] = [Globle getInstance].user;
    if (_type == 0) {
        params[@"type"] = @"";
    }else{
        params[@"type"] = [NSString stringWithFormat:@"%zd",_type];
    }
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappcheckedcompanylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        
        [self.ysh_tableView.mj_footer endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                CHCheckModel *model = [CHCheckModel objectWithKeyValues:dict];
                [self.checkDatas addObject:model];
            }
            [self.ysh_tableView reloadData];
            
        }
    }];

    
}

-(void)setupTitleMenu{
    
    NSArray *titles = @[@"待审核",@"已审核"];
    CHMenuView *menuView1 =[[ CHMenuView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, 40) withTitles:titles];
    menuView1.delegate = self;
    menuView1.isUnderline = NO;
    menuView1.bgColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    [self.view addSubview:menuView1];
    self.menuView1 = menuView1;
    
}

-(void)setupMainScrollView{
    
    
    UIScrollView *mainScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(self.menuView1.frame), UIScreenW, UIScreenH - CGRectGetMaxY(self.menuView1.frame) -64 - 44)];
    mainScrollview.delegate = self;
    mainScrollview.pagingEnabled = YES;
    mainScrollview.scrollEnabled = YES;
    mainScrollview.bounces = NO;
    mainScrollview.showsHorizontalScrollIndicator = NO;
    mainScrollview.showsVerticalScrollIndicator = NO;
    //    mainScrollview.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:mainScrollview];
    self.dsh_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, mainScrollview.h)];
    self.dsh_tableView.backgroundColor = KRGB(239, 239, 249);
    self.dsh_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.dsh_tableView.delegate = self;
    self.dsh_tableView.dataSource = self;
    // 下拉刷新
    self.dsh_tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getNoCheckData];
    }];
    self.dsh_tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreNoCheckData];
    }];
    self.dsh_tableView.mj_footer.automaticallyHidden = YES;
    [mainScrollview addSubview:self.dsh_tableView];
    [self.dsh_tableView.mj_header beginRefreshing];
    
    NSArray *titles = @[@"全部",@"通过",@"被拒绝"];
    CHMenuView *menuView2 =[[ CHMenuView alloc] initWithFrame:CGRectMake(UIScreenW, 1, UIScreenW, 40) withTitles:titles];
    menuView2.delegate = self;
    menuView2.isUnderline = NO;
    menuView2.bgColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    [mainScrollview addSubview:menuView2];
    self.menuView2 = menuView2;
    
    self.ysh_tableView = [[UITableView alloc] initWithFrame:CGRectMake(UIScreenW, 40, UIScreenW, mainScrollview.h-40 -1)];
    self.ysh_tableView.backgroundColor = KRGB(239, 239, 249);    // 下拉刷新
    self.ysh_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.ysh_tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getCheckData];
        
    }];
    self.ysh_tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreCheckData];
    }];
    self.ysh_tableView.mj_footer.automaticallyHidden = YES;
    self.ysh_tableView.delegate = self;
    self.ysh_tableView.dataSource = self;
    [mainScrollview addSubview:self.ysh_tableView];
    
    mainScrollview.contentSize = CGSizeMake(UIScreenW *2, 0);
    self.mainScrollView = mainScrollview;
}




#pragma mark - CHMenuViewDelegate
-(void)CHMenuView:(CHMenuView *)menuView didBtnClickWithTag:(NSInteger)tag{
    
    if (menuView == self.menuView1) {
        
       [self.mainScrollView setContentOffset:CGPointMake(( tag - 100) *UIScreenW, 0) animated:YES];
        
    }else{
        
        _type = tag -100;
        [self.ysh_tableView.mj_header beginRefreshing];
    }
}


#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.mainScrollView) {
        
        int tag = (int)scrollView.contentOffset.x/self.view.size.width +100 ;
     
        UIButton *btn = (UIButton *)[self.menuView1 viewWithTag:tag];
        
        
        [self.menuView1 titleBtnClick:btn];
    }
    
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.dsh_tableView) {
        return self.nocheckDatas.count;
    }else{
        return self.checkDatas.count;
    }
    return 0;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.dsh_tableView) {
        CHNoCheckCell *cell = [CHNoCheckCell cellForTableView:tableView];
        cell.model = self.nocheckDatas[indexPath.row];
        __weak typeof(self) weakSelf = self;
        cell.block = ^{
          
            CHAuditContentController *vc = [[CHAuditContentController alloc] init];
            CHNoCheckModel *model = weakSelf.nocheckDatas[indexPath.row];
            vc.companyid = model.companyid;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            
        };
        return cell;
    }else if (tableView == self.ysh_tableView){
        CHCheckCell *cell = [CHCheckCell cellForTableView:tableView];
        cell.model = self.checkDatas[indexPath.row];
        return cell;
    }
    return nil;
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (tableView == self.dsh_tableView) {
        
        id obj = [LongriseRouter openURL:@"CHAuditContentController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                CHNoCheckModel *model = self.nocheckDatas[indexPath.row];
                params[@"companyid"] = model.companyid;
               
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
        
    }else{
        
        id obj = [LongriseRouter openURL:@"CHAuditDetailController"];
        
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
               
                params[@"model"] = self.checkDatas[indexPath.row];
                
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }

        
    }
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"checkcompany" object:nil];
    
    
}

-(void)refreshData{
    
    [self.dsh_tableView.mj_header beginRefreshing];
    
    
}

-(void)dealloc{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
